const ScoreChuangxin = require('../model/actScoreChuangxin.model')
const ScoreWenti = require('../model/actScoreWenti.model')
const ScoreXueye = require('../model/actScoreXueye.model')
const ScoreSixiang = require('../model/actScoreSixiang.model')


class actScoreService {
    /**
     * 增加一条创新分数据
     */
    async createChuangxin (scoreData) {
        // 插入数据
        try {
            const res = await ScoreChuangxin.create(scoreData)
            return res
        } catch (error) {
            return {
                errorFlag: 'error',
                errorCode: error.parent.code,
                message: error.parent.sqlMessage,
            }
        }
    }

    /**
     * 修改一条创新分数据
     */
    async editChuangxin (editData, act_id, user_account) {
        // 插入数据
        try {
            const res = await ScoreChuangxin.update(editData, { where: { act_id, user_account } })
            return res
        } catch (error) {
            return {
                errorFlag: 'error',
                message: error,
            }
        }
    }

    /**
     * 增加一条文体分数据
     */
    async createWenti (scoreData) {
        // 插入数据
        try {
            const res = await ScoreWenti.create(scoreData)
            return res
        } catch (error) {
            return {
                errorFlag: 'error',
                errorCode: error,
                message: error,
            }
        }
    }

    /**
     * 修改一条文体分数据
     */
    async editWenti (editData, act_id, user_account) {
        // 插入数据
        try {
            const res = await ScoreWenti.update(editData, { where: { act_id, user_account } })
            return res
        } catch (error) {
            return {
                errorFlag: 'error',
                message: error,
            }
        }
    }

    /**
     * 增加一条思想分数据
     */
    async createSixiang (scoreData) {
        // 插入数据
        try {
            const res = await ScoreSixiang.create(scoreData)
            return res
        } catch (error) {
            return {
                errorFlag: 'error',
                errorCode: error.parent.code,
                message: error.parent.sqlMessage,
            }
        }
    }

    /**
     * 修改一条思想分数据
     */
    async editSixiang (editData, act_id, user_account) {
        // 插入数据
        try {
            const res = await ScoreSixiang.update(editData, { where: { act_id, user_account } })
            return res
        } catch (error) {
            return {
                errorFlag: 'error',
                message: error,
            }
        }
    }

    /**
     * 增加一条学业分数据
     */
    async createXueye (scoreData) {
        // 插入数据
        try {
            const res = await ScoreXueye.create(scoreData)
            return res
        } catch (error) {
            console.log(error)
            return {
                errorFlag: 'error',
                errorCode: error,
                message: error,
            }
        }
    }

    /**
    * 修改一条学业分数据
    */
    async editXueye (editData, act_id, user_account) {
        // 插入数据
        try {
            const res = await ScoreXueye.update(editData, { where: { act_id, user_account } })
            return res
        } catch (error) {
            return {
                errorFlag: 'error',
                message: error,
            }
        }
    }



    /**
     * 获取创新分列表
     */
    async getChuangxinList (whereOpt) {
        // console.log(whereOpt);
        // const whereOpt = {}
        // 短路运算 值存在就拷贝进whereOpt
        const res = await ScoreChuangxin.findAll({ where: whereOpt })
        return res
    }

    /**
     * 获取学业分列表
     */
    async getXueyeList ({ act_id, user_account, year, score_approval_status }) {
        const whereOpt = {}
        // 短路运算 值存在就拷贝进whereOpt
        act_id && Object.assign(whereOpt, { act_id })
        year && Object.assign(whereOpt, { year })
        user_account && Object.assign(whereOpt, { user_account })
        score_approval_status && Object.assign(whereOpt, { score_approval_status })
        const res = await ScoreXueye.findAll({ where: whereOpt })
        return res
    }


    /**
     * 获取思想分列表
     */
    async getSixiangList ({ act_id, user_account, year, score_approval_status }) {
        const whereOpt = {}
        // 短路运算 值存在就拷贝进whereOpt
        act_id && Object.assign(whereOpt, { act_id })
        year && Object.assign(whereOpt, { year })
        user_account && Object.assign(whereOpt, { user_account })
        score_approval_status && Object.assign(whereOpt, { score_approval_status })
        const res = await ScoreSixiang.findAll({ where: whereOpt })
        return res
    }


    /**
     * 获取文体分列表
     */
    async getWentiList ({ act_id, user_account, year, score_approval_status }) {
        const whereOpt = {}
        // 短路运算 值存在就拷贝进whereOpt
        act_id && Object.assign(whereOpt, { act_id })
        year && Object.assign(whereOpt, { year })
        user_account && Object.assign(whereOpt, { user_account })
        score_approval_status && Object.assign(whereOpt, { score_approval_status })
        const res = await ScoreWenti.findAll({ where: whereOpt })
        return res
    }

    /**
     * 修改四个分数的信息
     */
    async modifyScoreByActId (modifyData, act_id) {
        try {
            await ScoreChuangxin.update(modifyData, { where: { act_id, score_approval_status: 3 } })
            await ScoreXueye.update(modifyData, { where: { act_id, score_approval_status: 3 } })
            await ScoreSixiang.update(modifyData, { where: { act_id, score_approval_status: 3 } })
            await ScoreWenti.update(modifyData, { where: { act_id, score_approval_status: 3 } })
            return true
        } catch (error) {
            return {
                errorFlag: 'error',
                message: error,
            }
        }
    }
}

module.exports = new actScoreService()